<template>
  <el-dialog :title="form.id ? '编辑' : '新增'" :visible="show" :before-close="close">
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="编号" prop="code">
        <el-input v-model="form.code" type="text" placeholder="请输入" maxlength="20" show-word-limit></el-input>
      </el-form-item>
      <el-form-item label="角色名称" prop="name">
        <el-input v-model="form.name" type="text" placeholder="请输入" maxlength="20" show-word-limit></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </div>
  </el-dialog>
</template>
<script>
import {roleAdd} from '@/api/user'
export default {
  props: {
    show: {
      type: Boolean,
      default: false,
    },
    item: {
      type: Object,
      default: () => {},
    },
  },
  watch: {
    show: {
      handler(nv) {
        if (nv) {
          this.form = this.item || {};
        }
      },
      immediate: true,
    },
  },
  data() {
    return {
      form: {},
      rules: {
        name: {required: true, message: '请输入职位名称', trigger: 'blur'},
        code: {required: true, message: '请输入职位编码', trigger: 'blur'},
      }
    };
  },
  methods: {
    close() {
      this.$emit("update:show", false);
    },
    submit() {
        this.$refs['form'].validate( async valid =>{
            if(valid) { 
                try {
                    const res = await roleAdd(this.form);
                    if(res.code == 1) {
                        this.$emit("confirm")
                        this.close()
                    }
                } catch (error) {
                    
                }
            }
        })
    },
  },
};
</script>